#! /bin/sh
#
# git_auto_push.sh
#
git_push() {

  git status
  git_sttus=$(git status)

  echo
  #  echo "$git_sttus"
  failed=1 # True

  commitInfo="automatic push no commit info..."

  if [[ $git_sttus =~ "nothing to commit, working tree clean" ]]; then
    failed=0
    git pull
  fi

  echo

  modify_time='stat -c %Y $(pwd)'
  this_time=$(date +%s)

  date='date "%Y-%m-%d %H:%M:%S"'

  if [ $failed -eq 1 ]; then
    echo "------------------------------------------------------------------------------------------"
    echo
    read -p "提交信息:" info
    echo
    if [ -n "$info" ]; then
      commitInfo=$info
    fi

    git add .
    git commit -m "$commitInfo"

    echo "git fetch origin master"
    git fetch origin master
    echo "git merge origin/master"
    git_merge=$(git merge origin/master)

    if [[ $git_merge =~ "CONFLICT" ]]; then
      echo "------------------------------------------------------------------------------------------"
      echo -e "\033[31m  代码冲突  \033[0m"
      echo -e "\033[31m  $git_merge \033[0m"
      echo "------------------------------------------------------------------------------------------"
    else
      echo "------------------------------------------------------------------------------------------"
      echo "git push origin master:master"
      git push origin master:master
      git_log=$(git log -10 --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset - %C(yellow)%d%Creset %s %C(bold blue)<%an> %Cgreen (%cr) %Creset' --abbrev-commit)
      echo "------------------------------------------------------------------------------------------"
      echo "$git_log"
      echo "------------------------------------------------------------------------------------------"
    fi
  fi
}

git_push